package com.smartappsnetwork.personalexpenses;

import java.util.Vector;

import android.app.AlertDialog;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.app.FragmentActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.smartappsnetwork.personalexpenses.adapters.CategoryAdapter;
import com.smartappsnetwork.personalexpenses.adapters.CurrencyAdapter;
import com.smartappsnetwork.personalexpenses.adapters.PayModeAdapter;
import com.smartappsnetwork.personalexpenses.colorpicker.ColorPickerDialog;
import com.smartappsnetwork.personalexpenses.colorpicker.ColorPickerDialog.OnColorSelectedListener;
import com.smartappsnetwork.personalexpenses.dataaccess.CategoryDA;
import com.smartappsnetwork.personalexpenses.dataaccess.CurrencyDA;
import com.smartappsnetwork.personalexpenses.dataaccess.PayModeDA;
import com.smartappsnetwork.personalexpenses.dataobject.CategoryDO;
import com.smartappsnetwork.personalexpenses.dataobject.CurrencyDO;
import com.smartappsnetwork.personalexpenses.dataobject.PayModeDO;

public class SettingsActivity extends FragmentActivity implements OnClickListener
{
	TextView tvCurrency, tvAddCurrency, tvExportData, tvBackUpRestore, tvSpreadSheet, tvCategories, tvAddCategory, tvPayMode, tvAddPayMode;
	ExpandableHieightListView lvCurrency,lvCategory, lvPayMode;
	
	CurrencyAdapter currencyAdapter;
	CategoryAdapter categoryAdapter;
	PayModeAdapter payModeAdapter;
	
	
	Vector<CategoryDO> vecCategoryDO;
	Vector<PayModeDO> vecPayModeDO;
	Vector<CurrencyDO> vecCurrencyDO;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) 
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.settings_activity);
		initializeControls();
		
		lvCategory.setOnItemClickListener(new OnItemClickListener() 
		{

			@Override
			public void onItemClick(AdapterView<?> arg0, View view1, final int position, long arg3) 
			{

		        
		        final AlertDialog.Builder alertDialog = new AlertDialog.Builder(SettingsActivity.this);
				LinearLayout view = (LinearLayout) LayoutInflater.from(SettingsActivity.this).inflate(R.layout.add_category_dialog, null); 
				alertDialog.setView(view);
				final AlertDialog dialog = alertDialog.create();
				final TextView tvtitle = (TextView) view.findViewById(R.id.tvTitle);
				tvtitle.setText("Modify Category");
				final EditText edtCategoryName = (EditText) view.findViewById(R.id.edtCategoryName);
				final TextView tvColorCode = (TextView) view.findViewById(R.id.tvColorCode);
				tvColorCode.setBackgroundColor((int)vecCategoryDO.get(position).colorcode);
				
				final CategoryDO categoryDO = new CategoryDO();
				
				edtCategoryName.setText(vecCategoryDO.get(position).name);
				categoryDO.colorcode = vecCategoryDO.get(position).colorcode;
				
				tvColorCode.setOnClickListener(new OnClickListener() 
				{
					@Override
					public void onClick(View v) 
					{
				        ColorPickerDialog colorPickerDialog = new ColorPickerDialog(SettingsActivity.this, 123412, new OnColorSelectedListener() {
							
							@Override
							public void onColorSelected(int color) {
								tvColorCode.setBackgroundColor(color);
								categoryDO.colorcode = color;
							}
						});
				        colorPickerDialog.show();
					}
				});
				
				edtCategoryName.setText(vecCategoryDO.get(position).name);
				
				Button btnOk = (Button) view.findViewById(R.id.btnOk);
				Button btnCancel = (Button) view.findViewById(R.id.btnCancel);
				btnOk.setOnClickListener(new OnClickListener() 
				{
					@Override
					public void onClick(View v) 
					{
						if(edtCategoryName.getText().toString().equalsIgnoreCase(""))
						{
							Toast.makeText(getApplicationContext(), "Please enter the complete information!", Toast.LENGTH_SHORT).show();
						}
						else
						{
							final String currency = edtCategoryName.getText().toString();
							categoryDO.name = currency;
							categoryDO.cid = vecCategoryDO.get(position).cid;
							new CategoryDA().updateCategory(categoryDO);
							dialog.dismiss();
							categoryAdapter.refreshList(new CategoryDA().getAllCategory());
						}
					}
				});
				btnCancel.setOnClickListener(new OnClickListener() 
				{
					
					@Override
					public void onClick(View v) 
					{
						dialog.dismiss();
					}
				});
				dialog.show();
				
			
			}
		});
		
		
		
		lvPayMode.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View conView, final int position, long arg3) 
			{

		        
		        final AlertDialog.Builder alertDialog = new AlertDialog.Builder(SettingsActivity.this);
				LinearLayout view = (LinearLayout) LayoutInflater.from(SettingsActivity.this).inflate(R.layout.add_paymode_dialog, null); 
				alertDialog.setView(view);
				final AlertDialog dialog = alertDialog.create();
				final TextView tvtitle = (TextView) view.findViewById(R.id.tvTitle);
				tvtitle.setText("Modify PayMode");
				final EditText edtPayModeName = (EditText) view.findViewById(R.id.edtPayModeName);
				
				final PayModeDO payModeDO = new PayModeDO();
				
				edtPayModeName.setText(vecPayModeDO.get(position).name);
				
				
				Button btnOk = (Button) view.findViewById(R.id.btnOk);
				Button btnCancel = (Button) view.findViewById(R.id.btnCancel);
				btnOk.setOnClickListener(new OnClickListener() 
				{
					@Override
					public void onClick(View v) 
					{
						if(edtPayModeName.getText().toString().equalsIgnoreCase(""))
						{
							Toast.makeText(SettingsActivity.this, "Please enter the complete information!", Toast.LENGTH_SHORT).show();
						}
						else
						{
							final String currency = edtPayModeName.getText().toString();
						
							new Thread(new Runnable() {
								
								@Override
								public void run() 
								{
									
									payModeDO.name = currency;
									payModeDO.pid = vecPayModeDO.get(position).pid;
									
									new PayModeDA().updatePayMode(payModeDO);
											dialog.dismiss();
											payModeAdapter.refreshList(new PayModeDA().getAllPayModes());
								}
							}).start();
						}
					}
				});
				btnCancel.setOnClickListener(new OnClickListener() 
				{
					
					@Override
					public void onClick(View v) 
					{
						dialog.dismiss();
					}
				});
				dialog.show();
				
			
			}
		});
		
		lvCurrency.setOnItemClickListener(new OnItemClickListener() 
		{

			@Override
			public void onItemClick(AdapterView<?> arg0, View conView, final int position,long arg3) 
			{

				final AlertDialog.Builder alertDialog = new AlertDialog.Builder(SettingsActivity.this);
				LinearLayout view = (LinearLayout) LayoutInflater.from(SettingsActivity.this).inflate(R.layout.add_new_currency_dialog, null); 
				alertDialog.setView(view);
				final AlertDialog dialog = alertDialog.create();
				final TextView tvtitle = (TextView) view.findViewById(R.id.tvtitle);
				tvtitle.setText("Modify Currency");
				final EditText etCurrency = (EditText) view.findViewById(R.id.etCurrency);
				final EditText etCurrencySymbol = (EditText) view.findViewById(R.id.etCurrencySymbol);
				etCurrency.setText(vecCurrencyDO.get(position).name);
				etCurrencySymbol.setText(vecCurrencyDO.get(position).simbol);
				
				Button btnOk = (Button) view.findViewById(R.id.btnOk);
				Button btnCancel = (Button) view.findViewById(R.id.btnCancel);
				btnOk.setOnClickListener(new OnClickListener() 
				{
					@Override
					public void onClick(View v) 
					{
						if(etCurrency.getText().toString().equalsIgnoreCase("") || etCurrencySymbol.getText().toString().equalsIgnoreCase(""))
						{
							Toast.makeText(SettingsActivity.this, "Please enter the complete information!", Toast.LENGTH_SHORT).show();
						}
						else
						{
							final String currency = etCurrency.getText().toString();
							final String currencySymbol  = etCurrencySymbol.getText().toString();
						
							new Thread(new Runnable() {
								
								@Override
								public void run() 
								{
									CurrencyDO currencyDO = new CurrencyDO();
									currencyDO.name = currency;
									currencyDO.simbol = currencySymbol;
									currencyDO.currencyid = vecCurrencyDO.get(position).currencyid;
									
									new CurrencyDA().updateCurrency(currencyDO);
									dialog.dismiss();
									currencyAdapter.refresh(new CurrencyDA().getAllCurrency());
										
								}
							}).start();
						}
					}
				});
				btnCancel.setOnClickListener(new OnClickListener() 
				{
					
					@Override
					public void onClick(View v) 
					{
						dialog.dismiss();
					}
				});
				dialog.show();
				
			
			}
		});
	}

	private void initializeControls() 
	{
		tvCurrency = (TextView) findViewById(R.id.tvCurrency); 
		tvAddCurrency = (TextView) findViewById(R.id.tvAddCurrency);
		tvExportData = (TextView) findViewById(R.id.tvExportData); 
		tvBackUpRestore = (TextView) findViewById(R.id.tvBackupRestore); 
		tvSpreadSheet = (TextView) findViewById(R.id.tvSpreadsheet);
		tvCategories = (TextView) findViewById(R.id.tvCategories); 
		tvAddCategory = (TextView) findViewById(R.id.tvAddCategories);
		tvPayMode = (TextView) findViewById(R.id.tvPayMode);
		tvAddPayMode = (TextView) findViewById(R.id.tvAddPayMode);
		
		lvCurrency = (ExpandableHieightListView) findViewById(R.id.lvCurrency); 
		lvCategory = (ExpandableHieightListView) findViewById(R.id.lvCategories);
		lvPayMode = (ExpandableHieightListView) findViewById(R.id.lvPayMode);
		lvCurrency.setScrollContainer(false);
		
		lvCurrency.setExpanded(true);
		lvCategory.setExpanded(true);
		lvPayMode.setExpanded(true);
		
		vecCurrencyDO = new CurrencyDA().getAllCurrency();
		currencyAdapter = new CurrencyAdapter(SettingsActivity.this, vecCurrencyDO);
		lvCurrency.setAdapter(currencyAdapter);
		
		vecCategoryDO =  new CategoryDA().getAllCategory();
		categoryAdapter = new CategoryAdapter(SettingsActivity.this, vecCategoryDO);
		lvCategory.setAdapter(categoryAdapter);
		
		vecPayModeDO = new PayModeDA().getAllPayModes();
		payModeAdapter = new PayModeAdapter(SettingsActivity.this, vecPayModeDO);
		lvPayMode.setAdapter(payModeAdapter);
		
		
		
		
		tvCurrency.setOnClickListener(this);
		tvAddCurrency.setOnClickListener(this);
		tvExportData.setOnClickListener(this);
		tvBackUpRestore.setOnClickListener(this);
		tvSpreadSheet.setOnClickListener(this);
		tvCategories.setOnClickListener(this);
		tvAddCategory.setOnClickListener(this);
		tvPayMode.setOnClickListener(this);
		tvAddPayMode.setOnClickListener(this);
		
		lvCurrency.setVisibility(View.GONE);
		lvCategory.setVisibility(View.GONE);
		lvPayMode.setVisibility(View.GONE);
	}
	
	
	@Override
	public void onClick(View v) 
	{
		
		final AlertDialog.Builder alertDialog;
		LinearLayout view;
		Button btnOk;
		Button btnCancel;
		final AlertDialog dialog;
		final TextView tvtitle;
		
		switch (v.getId()) 
		{
		case R.id.tvCurrency:
				if(lvCurrency.getVisibility() == View.GONE)
				{
					lvCurrency.setVisibility(View.VISIBLE);
					tvCurrency.setCompoundDrawablesWithIntrinsicBounds(getResources().getDrawable(R.drawable.down_arrow), null, null, null);
				}
				else
				{
					lvCurrency.setVisibility(View.GONE);
					tvCurrency.setCompoundDrawablesWithIntrinsicBounds(getResources().getDrawable(R.drawable.right_arrow), null, null, null);
				}
			break;
		case R.id.tvAddCurrency:
			alertDialog = new AlertDialog.Builder(SettingsActivity.this);
			view = (LinearLayout) getLayoutInflater().inflate(R.layout.add_new_currency_dialog, null); 
			alertDialog.setView(view);
			dialog = alertDialog.create();
			tvtitle = (TextView) view.findViewById(R.id.tvtitle);
			tvtitle.setText("Add Currency");
			final EditText etCurrency = (EditText) view.findViewById(R.id.etCurrency);
			final EditText etCurrencySymbol = (EditText) view.findViewById(R.id.etCurrencySymbol);
			btnOk = (Button) view.findViewById(R.id.btnOk);
			btnCancel = (Button) view.findViewById(R.id.btnCancel);
			btnOk.setOnClickListener(new OnClickListener() 
			{
				
				@Override
				public void onClick(View v) 
				{
					if(etCurrency.getText().toString().equalsIgnoreCase("") || etCurrencySymbol.getText().toString().equalsIgnoreCase(""))
					{
						Toast.makeText(SettingsActivity.this, "Please enter the complete information!", Toast.LENGTH_SHORT).show();
					}
					else
					{
						final String currency = etCurrency.getText().toString();
						final String currencySymbol  = etCurrencySymbol.getText().toString();
					
						new Thread(new Runnable() {
							
							@Override
							public void run() 
							{
								CurrencyDO currencyDO = new CurrencyDO();
								currencyDO.name = currency;
								currencyDO.simbol = currencySymbol;
								
								new CurrencyDA().insertCurrency(currencyDO);
								
								new Handler(getMainLooper()).postDelayed(new Runnable() 
								{
									
									@Override
									public void run() {
										dialog.dismiss();
										
										currencyAdapter.refresh(new CurrencyDA().getAllCurrency());
										
									}
								}, 500);
							}
						}).start();
					}
				}
			});
			btnCancel.setOnClickListener(new OnClickListener() 
			{
				
				@Override
				public void onClick(View v) 
				{
					dialog.dismiss();
				}
			});
			dialog.show();
			
			break;
		case R.id.tvCategories:
//			if(lvCategory.getVisibility() == View.GONE)
//			{
//				lvCategory.setVisibility(View.VISIBLE);
//				tvCategories.setCompoundDrawablesWithIntrinsicBounds(getResources().getDrawable(R.drawable.down_arrow), null, null, null);
//			}
//			else
//			{
//				lvCategory.setVisibility(View.GONE);
//				tvCategories.setCompoundDrawablesWithIntrinsicBounds(getResources().getDrawable(R.drawable.right_arrow), null, null, null);
//			}
			
			startActivity(new Intent(SettingsActivity.this, CategoriesActivity.class));
			break;
		case R.id.tvAddCategories:
	        
	        alertDialog = new AlertDialog.Builder(SettingsActivity.this);
			view = (LinearLayout) getLayoutInflater().inflate(R.layout.add_category_dialog, null); 
			alertDialog.setView(view);
			dialog = alertDialog.create();
			tvtitle = (TextView) view.findViewById(R.id.tvTitle);
			tvtitle.setText("Modify Category");
			final EditText edtCategoryName = (EditText) view.findViewById(R.id.edtCategoryName);
			final TextView tvColorCode = (TextView) view.findViewById(R.id.tvColorCode);
			
			final CategoryDO categoryDO = new CategoryDO();
			tvColorCode.setOnClickListener(new OnClickListener() 
			{
				@Override
				public void onClick(View v) 
				{
					
					  ColorPickerDialog colorPickerDialog = new ColorPickerDialog(SettingsActivity.this, 123412, new OnColorSelectedListener() {
							
							@Override
							public void onColorSelected(int color) {
								 tvColorCode.setBackgroundColor(color);
					             categoryDO.colorcode = color;
							}
						});
				        colorPickerDialog.show();
				}
			});
			
			
			btnOk = (Button) view.findViewById(R.id.btnOk);
			btnCancel = (Button) view.findViewById(R.id.btnCancel);
			btnOk.setOnClickListener(new OnClickListener() 
			{
				@Override
				public void onClick(View v) 
				{
					if(edtCategoryName.getText().toString().equalsIgnoreCase(""))
					{
						Toast.makeText(SettingsActivity.this, "Please enter the complete information!", Toast.LENGTH_SHORT).show();
					}
					else
					{
						final String categoryName = edtCategoryName.getText().toString();
					
						new Thread(new Runnable() {
							
							@Override
							public void run() 
							{
								categoryDO.name = categoryName;
								
								new CategoryDA().insertCategory(categoryDO);
								
								new Handler(getMainLooper()).postDelayed(new Runnable() 
								{
									
									@Override
									public void run() {
										dialog.dismiss();
										
										categoryAdapter.refreshList(new CategoryDA().getAllCategory());
									}
								}, 500);
							}
						}).start();
					}
				}
			});
			btnCancel.setOnClickListener(new OnClickListener() 
			{
				
				@Override
				public void onClick(View v) 
				{
					dialog.dismiss();
				}
			});
			dialog.show();
			break;
			
		case R.id.tvPayMode:
			if(lvPayMode.getVisibility() == View.GONE)
			{
				lvPayMode.setVisibility(View.VISIBLE);
				tvPayMode.setCompoundDrawablesWithIntrinsicBounds(getResources().getDrawable(R.drawable.down_arrow), null, null, null);
			}
			else
			{
				lvPayMode.setVisibility(View.GONE);
				tvPayMode.setCompoundDrawablesWithIntrinsicBounds(getResources().getDrawable(R.drawable.right_arrow), null, null, null);
			}
		break;
		
		case R.id.tvAddPayMode:
			
			 alertDialog = new AlertDialog.Builder(SettingsActivity.this);
				view = (LinearLayout) getLayoutInflater().inflate(R.layout.add_paymode_dialog, null); 
				alertDialog.setView(view);
				dialog = alertDialog.create();
				tvtitle = (TextView) view.findViewById(R.id.tvTitle);
				tvtitle.setText("Add PayMode");
				final EditText edtPayModeName = (EditText) view.findViewById(R.id.edtPayModeName);
				
				final PayModeDO payModeDO = new PayModeDO();
				
				
				btnOk = (Button) view.findViewById(R.id.btnOk);
				btnCancel = (Button) view.findViewById(R.id.btnCancel);
				btnOk.setOnClickListener(new OnClickListener() 
				{
					@Override
					public void onClick(View v) 
					{
						if(edtPayModeName.getText().toString().equalsIgnoreCase(""))
						{
							Toast.makeText(SettingsActivity.this, "Please enter the complete information!", Toast.LENGTH_SHORT).show();
						}
						else
						{
							final String paymode = edtPayModeName.getText().toString();
						
							new Thread(new Runnable() {
								
								@Override
								public void run() 
								{
									payModeDO.name = paymode;
									
									new PayModeDA().insertPaymode(payModeDO);
									
									new Handler(getMainLooper()).postDelayed(new Runnable() 
									{
										
										@Override
										public void run() {
											dialog.dismiss();
											
											payModeAdapter.refreshList(new PayModeDA().getAllPayModes());
										}
									}, 500);
								}
							}).start();
						}
					}
				});
				btnCancel.setOnClickListener(new OnClickListener() 
				{
					
					@Override
					public void onClick(View v) 
					{
						dialog.dismiss();
					}
				});
				dialog.show();
			break;

		default:
			break;
		}
		
	}

}
